package cn.m3a1.creditsystem.service;

import cn.m3a1.creditsystem.bean.TrainingPlan;
import cn.m3a1.creditsystem.bean.TrainingPlanExample;
import cn.m3a1.creditsystem.dao.TrainingPlanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author wyj
 * @date 2020/2/12
 * @time 下午2:11
 */
@Service
@Transactional
public class TrainingPlanService {
    @Autowired
    TrainingPlanMapper trainingPlanMapper;
    public Boolean inserts(List<List<String>> list) {
//        course_nature  课程性质
//        course_code 课程代码
//        course_category 课程类别
//        student_credit 学分
//        semester 学期
//        college 学院
//        grade_year 年级
//        professional_title 专业名称

        for (int i = 1; i < list.size(); i++) {
            TrainingPlan trainingPlan = new TrainingPlan();
            List<String> lo = list.get(i);
            trainingPlan.setCourseNature(lo.get(0));
            trainingPlan.setCourseCode(lo.get(1));
            trainingPlan.setCourseName(lo.get(2));
            trainingPlan.setCourseCategory(lo.get(3));
            trainingPlan.setStudentCredit(Double.parseDouble(lo.get(15)));
            trainingPlan.setSemester(Integer.parseInt(lo.get(16)));
            trainingPlan.setCollege(lo.get(21));
            trainingPlan.setGradeYear(lo.get(27));
            trainingPlan.setProfessionalTitle(lo.get(28));
            trainingPlanMapper.insert(trainingPlan);
        }

        return true;
    }
    public List<TrainingPlan> selecttra(TrainingPlan trainingPlan) {
        TrainingPlanExample trainingPlanExample = new TrainingPlanExample();
        TrainingPlanExample.Criteria criteria = trainingPlanExample.createCriteria();
        if (trainingPlan.getCourseNature()!=null){
            criteria.andCourseNatureLike("%"+trainingPlan.getCourseNature().trim()+"%");
        }
        if (trainingPlan.getCourseCode()!=null){
            criteria.andCourseCodeLike("%"+trainingPlan.getCourseCode().trim()+"%");
        }
        if (trainingPlan.getCourseName()!=null){
            criteria.andCourseNameLike("%"+trainingPlan.getCourseName().trim()+"%");
        }
        if (trainingPlan.getGradeYear()!=null){
            criteria.andGradeYearLike("%"+trainingPlan.getGradeYear().trim()+"%");
        }
        if (trainingPlan.getProfessionalTitle()!=null){
            criteria.andProfessionalTitleLike("%"+trainingPlan.getProfessionalTitle().trim()+"%");
        }
        List<TrainingPlan> trainingPlans = trainingPlanMapper.selectByExample(trainingPlanExample);
        return  trainingPlans;
    }

    public void uptable(String tableName){
        System.out.println(tableName);
        String  mytable ="";
        if ("sc".equals(tableName)){
            mytable="school_schedule";
            trainingPlanMapper.truncateTable(mytable);
            trainingPlanMapper.truncateTable(mytable+"_model");
        }else if ( "st".equals(tableName)){
            mytable="student_schedule";
            trainingPlanMapper.truncateTable(mytable);
            trainingPlanMapper.truncateTable(mytable+"_model");
        }else if ("t".equals(tableName)){
            mytable="training_plan";
            trainingPlanMapper.truncateTable(mytable);
        }else if ("sr".equals(tableName)){
            mytable="student_result";
            trainingPlanMapper.truncateTable(mytable);
        }else if ("all".equals(tableName)){
            mytable="school_schedule";
            trainingPlanMapper.truncateTable(mytable);
            trainingPlanMapper.truncateTable(mytable+"_model");

            mytable="student_schedule";
            trainingPlanMapper.truncateTable(mytable);
            trainingPlanMapper.truncateTable(mytable+"_model");

            mytable="student_result";
            trainingPlanMapper.truncateTable(mytable);

            mytable="training_plan";
            trainingPlanMapper.truncateTable(mytable);

        }

    }
}
